package xj.graph2d;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.io.IOException;

import javax.swing.filechooser.FileFilter;

import xj.util.gui.ExtensionFileFilter;

abstract public class AbstractEncoder implements Encoder {

  // image types
  public static final int PNG = 101; // Portable Network Graphics

  public static final int JPEG = 102; // Joint Photographic Engineering Group

  public static final int TIFF = 103; // Tagged Image File Format

  public static final int PPM = 104; // Portable Pix Map

  public static final int GIF = 105; // Graphics Interchange Format

  public static final int PDF = 201; // Portable Document Format

  public static final int EPS = 202; // PostScript and Encapsulated

  // PostScript

  public static final int SVG = 203; // Scalable Vector Graphics

  public static final int CGM = 204; // Computer Graphics Metafile

  public static final int EMF = 205; // Enhanced Meta Format

  public static final int SWF = 206; // Shock Wave Format

  public String getName() {
    return name;
  }

  public String getFileExtension() {
    return fileExtension;
  }

  public String getFileDescription() {
    return fileDescription;
  }

  public FileFilter getFileFilter() {
    return fileFilter;
  }

  public void encode(String filename) throws IOException {
    // System.out.println("AbstractEncoder.encode() start");

    if (view != null) {
      Rectangle2D r = null;
      Document doc = view.getDocument();
      if (doc != null) {
	r = doc.getBounds();
      }
      // System.out.println("AbstractEncoder.encode() bounds=" + r);

      if (r == null) {
	DrawCanvas canvas = view.getCanvas();
	r = canvas.getVirtualCanvasRectangle();
      }

      // System.out.println("AbstractEncoder.encode() vrect=" + r);
      encode(filename, r);
    }

    // System.out.println("AbstractEncoder.encode() end");
  }

  public void setWaterMark(boolean wm) {
    waterMark = wm;
  }

  public boolean isUseAntialias() {
    return useAntialias;
  }

  public void setUseAntialias(boolean useAntialias) {
    this.useAntialias = useAntialias;
  }

  public boolean isFractionalMetrics() {
    return fractionalMetrics;
  }

  public void setFractionalMetrics(boolean fractionalMetrics) {
    this.fractionalMetrics = fractionalMetrics;
  }

  public boolean isTransparent() {
    return transparent;
  }

  public void setTransparent(boolean transparent) {
    this.transparent = transparent;
  }

  abstract public void encode(String filename, Rectangle2D r)
      throws IOException;

  protected AbstractEncoder() {
  }

  protected AbstractEncoder(String name, String fileExtension,
      String fileDescription, View view) {
    this.name = name;
    this.fileExtension = fileExtension;
    this.fileDescription = fileDescription;
    this.view = view;
    if (fileExtension != null) {
      if (fileDescription == null) {
	fileDescription = fileExtension + "files (*." + fileExtension + ")";
      }
      fileFilter = new ExtensionFileFilter(fileExtension, fileDescription);
    }
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setView(View view) {
    this.view = view;
  }

  public void setFileType(String fileExtension, String fileDescription) {
    this.fileExtension = fileExtension;
    this.fileDescription = fileDescription;
    if (fileExtension != null) {
      if (fileDescription == null) {
	fileDescription = fileExtension + "files (*." + fileExtension + ")";
      }
      fileFilter = new ExtensionFileFilter(fileExtension, fileDescription);
    }
  }

  public void setImageType(String type) {
  }

  public static final String WATER_MARK_STRING1 = "VisualModeler 5.0";

  public static final String WATER_MARK_STRING2 = "Evaluation Copy";

  protected void drawWaterMark(Graphics2D g2, float x0, float y0, float w,
      float h) {
    if (waterMark) {
      String waterMark1 = WATER_MARK_STRING1 + " " + WATER_MARK_STRING2;
      String waterMark2 = WATER_MARK_STRING2 + " " + WATER_MARK_STRING1;
      g2.setColor(DrawShapeConstants.waterMarkColor);
      g2.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
      int i = 0;
      for (float x = x0, y = y0; y < y0 + h; y += 44, x += 36, i++) {
	if (i % 2 == 0) {
	  g2.drawString(waterMark1, x, y + 36);
	} else {
	  g2.drawString(waterMark2, x, y + 36);
	}
      }
    }
  }

  protected void setRenderingHints(Graphics2D g2) {
    if (isUseAntialias()) {
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	  RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
	  RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    } else {
      g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
	  RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	  RenderingHints.VALUE_ANTIALIAS_OFF);
    }
    if (isFractionalMetrics()) {
      g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
	  RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    } else {
      g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
	  RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
    }
  }

  protected String name;

  protected String fileExtension;

  protected String fileDescription;

  protected FileFilter fileFilter;

  protected View view;

  protected boolean waterMark = false;

  protected boolean transparent = true;

  protected boolean useAntialias = true;

  protected boolean fractionalMetrics = true;

}
